﻿using System;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace mojoPortal.Web.RS.CRM.Controls
{
    public partial class MyAgentListControl : RSBaseUserControl
    {

        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["SELECTED_AGENTID_IS_MY_DOWNLINE_LEAD"] != null)
                {
                    btnAddNew.Visible = false;
                }
            }
        }
        //protected void ddlAgent_OnSelectedIndexChanged(object sender, EventArgs e)
        //{
        //    RSUserIdentity userIdentity = GetRSUserIdentity();
        //    string team = ddlAgent.SelectedValue;
        //    if (team == "Agent")
        //        LoadAgentList(userIdentity.RSCompanyId);
        //    if (team == "CEO")
        //        LoadCEOList(userIdentity.RSCompanyId);
        //    if (team == "LicAgent")
        //        LoadLicAgentList(userIdentity.RSCompanyId);
        //}
        protected void lnkViewClient_Click(object sender, CommandEventArgs e)
        {
            int EmployeeID = Convert.ToInt32(e.CommandArgument.ToString());
            if (EmployeeID > 0)
            {
                Session["SELECTED_AGENTID_IS_MY_DOWNLINE_LEAD"] = EmployeeID;
                Response.Redirect("RSMyClients.aspx");
            }
        }
        protected void lnkViewAssociates_Click(object sender, CommandEventArgs e)
        {
            int EmployeeID = Convert.ToInt32(e.CommandArgument.ToString());
            if (EmployeeID > 0)
            {
                Session["SELECTED_AGENTID_IS_MY_DOWNLINE_LEAD"] = EmployeeID;
                Response.Redirect("RSMyAgents.aspx");
            }
        }

        protected void gvAgent_Edit(object sender, CommandEventArgs e)
        {
            int EmployeeID = Convert.ToInt32(e.CommandArgument.ToString());
            if (EmployeeID > 0)
                Response.Redirect("RSAgentDetail.aspx?EmployeeID=" + EmployeeID);
        }
        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if(userIdentity.RSCanWrite)
                Response.Redirect("~/RS/CRM/RSAgentDetail.aspx?action=add");
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }

        public bool LoadMyAgentList(int companyid, int reportsto)
        {
            bool bHasValue = true;

            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;
            String sqlSELECT = "SELECT * FROM [dbo].[rs_tblEmployee] WHERE CompanyID=@CompanyID AND ReportsTo=@reportsto";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@reportsto";
            param.Value = reportsto;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            Session["DATATABLE"] = ds.Tables[0];
            gvAgentList.DataSource = ds;
            gvAgentList.DataBind();
            myConnection.Close();

            if (gvAgentList.Rows.Count == 0)
            {
                bHasValue = false;
                return bHasValue;
            }
            return bHasValue;
        }
        
        private string ConvertSortDirectionToSql(SortDirection sortDirection)
        {
            string newSortDirection = String.Empty;

            switch (sortDirection)
            {
                case SortDirection.Ascending:
                    newSortDirection = "ASC";
                    break;

                case SortDirection.Descending:
                    newSortDirection = "DESC";
                    break;
            }
            return newSortDirection;
        }

        protected void gvAgentList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvAgentList.PageIndex = e.NewPageIndex;
            gvAgentList.DataBind();
        }

        protected void gvAgentList_Sorting(object sender, GridViewSortEventArgs e)
        {
            DataTable dataTable = Session["DATATABLE"] as DataTable;
            if (dataTable != null)
            {
                DataView dataView = new DataView(dataTable);
                dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

                gvAgentList.DataSource = dataView;
                gvAgentList.DataBind();
            }
        }

    }
}